//
//  WmcWechatShare.m
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/14.
//

#import "WmcWechatShare.h"

#import <WechatOpenSDK/WXApi.h>
#import <WechatOpenSDK/WXApiObject.h>

@implementation WmcWechatShare

+ (void)shareText:(NSString *)title
      description:(NSString *)description
            scene:(int)scene
       completion:(void(^)(BOOL))completion {
    WXMediaMessage *message = WXMediaMessage.new;
    message.title = title;
    message.description = description;
    
    SendMessageToWXReq *req = SendMessageToWXReq.new;
    req.message = message;
    req.bText = YES;
    req.scene = scene;
    
    [WXApi sendReq:req completion:completion];
}

+ (void)shareImage:(UIImage *)image
             title:(NSString *)title
       description:(NSString *)description
        messageExt:(NSString *)messageExt
             scene:(int)scene completion:(void(^)(BOOL))completion {
    NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
    
    WXImageObject *imageObject = [WXImageObject object];
    imageObject.imageData = imageData;
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.mediaObject = imageObject;
    message.title = title;
    message.description = description;
    message.messageExt = messageExt;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = scene;
    [WXApi sendReq:req completion:completion];
}

+ (void)shareLink:(NSString *)link
            title:(NSString *)title
      description:(NSString *)description
       thumbImage:(UIImage *)thumbImage
            scene:(int)scene completion:(void(^)(BOOL))completion {
    WXWebpageObject *webpageObject = [WXWebpageObject object];
    webpageObject.webpageUrl = link;
    
    WXMediaMessage *mediaMessage = [WXMediaMessage message];
    mediaMessage.title = title;
    mediaMessage.description = description;
    mediaMessage.mediaObject = webpageObject;
    [mediaMessage setThumbImage: thumbImage];
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = mediaMessage;
    req.scene = scene;
    [WXApi sendReq:req completion:completion];
}


+ (void)shareFile:(NSString *)path
    fileExtension:(NSString *)fileExtension
            title:(NSString *)title
      description:(NSString *)description
       thumbImage:(UIImage *)thumbImage
            scene:(int)scene
       completion:(void(^)(BOOL))completion  {
    
    WXFileObject *fileObj = [WXFileObject object];
    fileObj.fileData = [[NSData alloc] initWithContentsOfFile:path];
    fileObj.fileExtension = fileExtension;
    
    WXMediaMessage *wxMediaMessage = [WXMediaMessage message];
    wxMediaMessage.title = title;
    wxMediaMessage.description = description;
    wxMediaMessage.messageExt = fileExtension;
    wxMediaMessage.mediaObject = fileObj;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = wxMediaMessage;
    req.scene = scene;
    [WXApi sendReq:req completion:completion];
}



+ (BOOL)isInstalled {
    return [WXApi isWXAppInstalled];
}

@end
